package com.seven.dao;


import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

import com.seven.util.Config;





public class BaseDao {
	private static Connection conn = null;
	private static Statement st = null;
	private static ResultSet rst = null;
	private static PreparedStatement pst = null;
	private static String driver = Config.driver;
	private static String url = Config.url;
	private static String username = Config.user;
	private static String password = Config.password;
	


	
	
	
	public static Connection getConnection1(){
		try {
			if(conn == null){
				Class.forName("com.mysql.jdbc.Driver");
				conn = DriverManager.getConnection(
						"jdbc:mysql://localhost:3306/bicycle", "root","root");
				// "jdbc:mysql://127.0.0.1:3306/mmr", "root", "maomuren"
				System.out.println("conn" + conn);
				return conn;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}// com.mysql.jdbc.Driver
		
		return conn;
	}
	


	
	
	public int update(String sql,Object...ob) {
		try {
		
			conn  = getConnection1();
			pst = conn.prepareStatement(sql);
			for (int i = 0; i < ob.length; i++) {
				pst.setObject(i+1,ob[i]);
			}
			int row = pst.executeUpdate();

			
			return row;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			
			close();
		}
		return -1;
	}

	public ResultSet select(String sql , Object...objects){
		try {
			//加载驱动并建立连接
			conn = getConnection1();
			//检验
			pst = conn.prepareStatement(sql);
			//给占位符赋值
			for (int i = 0; i < objects.length; i++) {
				if (objects != null) {
					pst.setObject(i+1, objects[i]);
				}
			}
			rst = pst.executeQuery();
			return rst;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
		//此处不关闭
	}
	
	
	
	public ResultSet query(String sql) {
		conn = getConnection1();
		
		try {
			st  =  conn.createStatement();
			 rst = st.executeQuery(sql);
			return rst;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			
		}
		return null;
		
	}
	
	
	
	//
	public <T> List<T> query2(String sql,Class<T> cla,Object...ob){
		conn = getConnection1();
		List<T> list = new ArrayList<T>();
		System.out.println("SQL:"+sql);
		System.out.println("param:"+Arrays.toString(ob));
		try {
		    pst = conn.prepareStatement(sql);
		    if(ob!=null){
		    	for(int i=0; i<ob.length;i++){
		    		pst.setObject(i+1, ob[i]);
		    	}
		    	
		    }
			rst= pst.executeQuery();
			ResultSetMetaData rsmd = rst.getMetaData();
			int columncount = rsmd.getColumnCount();
			
			while(rst.next()){
				try {
					
					T t = cla.newInstance();
					for (int i = 0; i < columncount; i++) {
						//��ȡ����
						String columnname  = rsmd.getColumnName(i+1);
						//��ȡ�����Ӧ��ֵ
						String  obj = rst.getString(columnname);
						//�ַ�ƴ�ӳ�Set����
						String wayName = "set"+columnname.substring(0, 1).toUpperCase()+columnname.substring(1);
						//�������,��������������
						Field field = cla.getDeclaredField(columnname);
						System.out.println(columncount+"***"+field.getType());
						//��ݷ�����͸������Ի��method����
						Method method = cla.getDeclaredMethod(wayName, field.getType());
						//ִ�з���
//						System.out.println(columncount+"++++"+field.getType());
//						System.out.println(wayName+"00000");
						if(field.getType().toString().equals("int")){
							if (obj!=null) {
								method.invoke(t, Integer.parseInt(obj));
							}
							
						}else if(field.getType().toString().contains("String")){							
							method.invoke(t, obj);
						}else{		
								method.invoke(t, Date.valueOf(obj));	
						}
						
						
					}
					list.add(t);	
				} catch (Exception e) {
					e.printStackTrace();
				}//ʵ�����
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(list.size()!=0){
			System.out.println(list.size()+"))))");
			return list;
		}
		return null;
	}
	
	public void close() {
		try {
			if(rst!=null){
				rst.close();
				rst = null;
			}
			if(st!=null){
				st.close();	
				st= null;
			}
			if(conn!=null){
				conn.close();
				conn = null;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
}
